<?php
/**
物业端公告
 */
namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Common\push;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NoticeController extends Controller{

    private $app_key = '09ae7f39eb877836195b52df';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='28783d0dacf52cfd12d952a6';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址


    private $appios_key = '1aef5ddc66c9bc4e821e84a3';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='e0a01a1f2e56db54bf06f3fb';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址


    public function Addpush($notice_village,$notice_type){
        $push   = new push($this ->app_key,$this ->master_secret,$this->url);
        $pushios   = new push($this ->appios_key,$this ->masterios_secret,$this->urlios);
        switch ($notice_type){
            case "2":
                try {
                    $push->send_pub($notice_village, '你有一条新活动公告请注意查看', '2');
                    $pushios->send_pub($notice_village, '你有一条新活动公告请注意查看', '2');
                }catch (\Exception $exception){
                    exit();
                }
                break;
            case "3":
                try {
                    $push->send_pub($notice_village, '你有一条新生活公告请注意查看', '2');
                    $pushios->send_pub($notice_village, '你有一条新生活公告请注意查看', '2');
                }catch (\Exception $exception){
                    exit();
                }
                break;
            case "4":
                try {
                    $push->send_pub($notice_village, '你有一条新商品公告请注意查看', '2');
                    $pushios->send_pub($notice_village, '你有一条新商品公告请注意查看', '2');
                }catch (\Exception $exception){
                    exit();
                }
                break;
            case "5":
                try {
                    $push->send_pub($notice_village, '你有一条新其它类型公告请注意查看', '2');
                    $pushios->send_pub($notice_village, '你有一条新其它类型公告请注意查看', '2');
                }catch (\Exception $exception){
                    exit();
                }
                break;
            default:
                break;
        }
    }
    public function AddNotice(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $notice_village  = $request ->input('notice_village');  //小区ID
            $notice_title    = $request ->input('notice_title');    //标题
            $notice_content  = $request ->input('notice_content');  //内容
            $notice_image    = $request ->input('notice_image');    //JPG
            $notice_type    = $request ->input('notice_type');
            $time            = date('Y-m-d H:i:s');
            $select          = DB::table('notice')
                             ->where('notice_title',$notice_title)
                             ->where('notice_content',$notice_content)
                             ->get();
            if($select  == true){
                return json::json('failure', '发布的数据已经存在', '');
            }else {
                $data = DB::table('notice')
                    ->select('notice_village', 'notice_title', 'notice_content', 'notice_image','notice_type', 'notice_create_time')
                    ->insert([
                        'notice_village' => $notice_village, 'notice_title' => $notice_title, 'notice_content' => $notice_content,
                        'notice_image' => $notice_image,'notice_create_time'=>$time,'notice_type'=>$notice_type
                    ]);

                if ($data == true) {
                    echo json::json('success', '小区公告发布成功', $arr);
                    $this ->Addpush($notice_village,$notice_type);
                } else {
                    echo json::json('failure', '由于网络或者不正当原因失败', $arr);
                }
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }



    public function UpdateNotice(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $notice_id     = $request ->input('notice_id'); //公告ID
          //  $notice_village  = $request ->input('notice_village');  //小区ID
            $notice_title    = $request ->input('notice_title');    //标题
            $notice_content  = $request ->input('notice_content');  //内容
            $notice_image    = $request ->input('notice_image');    //JPG
            $time            = date('Y-m-d H:i:s');
            $data            = DB::table('notice')
                             ->where('notice_id',$notice_id)
                             ->update([
                                 'notice_title' => $notice_title, 'notice_content' => $notice_content,
                                 'notice_image' => $notice_image,'notice_update_time'=>$time
                             ]);
            if ($data == true) {
                return json::json('success', '小区公告修改成功', $arr);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', $arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }




    public function DelNotice(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $notice_id     = $request ->input('notice_id'); //公告ID
            $data      = DB::table('notice')
                       ->where('notice_id',$notice_id)
                       ->delete();
            if ($data == true) {
                return json::json('success', '删除成功', $data);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', $arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //获取全部公告
    public function AllNotice(Request $request){
        if($request ->isMethod('post')){
            $village_id         = $request ->input('village_id');
            $failure     =[];
            $data    = DB::table('notice')
                     ->select('notice_village','notice_id','notice_title','notice_content','notice_image','notice_create_time','notice_type')
                     ->where('notice_village',$village_id)
                     ->orderby('notice_id','ASE')
                     ->get();
            foreach ($data as $value){
                if(empty($value ->notice_image)){
                    $notice_image   = [];
                }else{
                    $notice_image   = $value ->notice_image;
                }
                $dd[]  = array(
                    'notice_village'   =>$value ->notice_village,
                    'notice_id'        =>$value ->notice_id,
                    'notice_title'    =>$value ->notice_title,
                    'notice_content'  =>$value ->notice_content,
                    'notice_create_time' => $value ->notice_create_time,
                    'notice_type'         =>$value ->notice_type,
                    'notice_image'       =>$notice_image
                );
            }

            if ($data == true) {
                return json::json('success', '获取小区公告成功', $dd);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', $failure);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    //获取公告详情
    public function SelectNotice(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $notice_id     = $request ->input('notice_id'); //公告ID
            $data          = DB::table('notice')
                           ->select('notice_title','notice_content','village_name','property_name','notice_image','notice_create_time')
                           ->leftjoin('village','notice.notice_village','=','village.village_id')
                           ->leftjoin('property','village.village_property','=','property.property_id')
                           ->where('notice_id',$notice_id)
                           ->get();
            foreach ($data as $value){
                $notice_title    = $value->notice_title;
                $time            = $value ->notice_create_time;
                $notice_content  = $value ->notice_content;
                if(empty($value ->notice_image)){
                    $notice_image  =[];
                }else{
                    $notice_image    = explode(',',$value->notice_image);
                }

                $address         = $value->property_name.'-'.$value->village_name;
                $da              = array('title'=>$notice_title,'time'=>$time,'content'=>$notice_content,
                    'image'=>$notice_image,'address'=>$address);
            }
            if ($data == true) {
                return json::json('success', '获取小区公告详情成功', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', $arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    public function noticecs(Request $request){
        if($request ->isMethod('post')){
        /*    $str = file_get_contents("http://www.mukitech.com/wuye/app/public/tenement/AddNotice");
            echo $str;*/
          /*  $arr = json_decode($str, 1);

            print_r($arr);*/
           /* $data    = DB::table('user')->select('phone')->where('user_village',17)->get();
            $push   = new push();
            foreach ($data as $v){
                $id[] = $v ->phone;

            }
            for ($i=0;$i<count($id);$i++){

                 $push->send_pub($id[$i],'你有一条件','2');

            }*/

         //   echo  $id;
        }
    }


}